#-*-coding:utf-8-*-

import sys
import time
import os
import re



def getNukeFileInfo(dirpath):
    dirlist = os.listdir(dirpath)
    file0=(dirlist[0])
    filelast = (dirlist[-1])
    reg = re.compile('(.*\D)(\d+)(\..*)')
    m0 = re.search(reg, file0)
    mlast=re.search(reg, filelast)
    num0=int(m0.group(2))
    lenth = len(m0.group(2))

    numlast=int(mlast.group(2))

    filename=m0.group(1)
    nukefile= filename+'#'*lenth+m0.group(3)




    return (filename,nukefile,num0,numlast)



#dirpath='P:/GJQT2/Shot_work/Composite/test/GJ2-54-01-001_fin_v001'






#args
#PATH='P:/GJQT2/Shot_work/Composite/test/GJ2-54-01-001_fin_v001'
#FPS=25
#OUTPATH='P:/GJQT2/Shot_work/Composite/test/output'

#Root Setting

def convertMov(PATH,FPS,OUTPATH):
    root=nuke.Root()
    root['fps'].setValue(int(FPS))
    root['format'].setValue('HD_720')


    read1=nuke.createNode('Read')
    nukefileInfo=getNukeFileInfo(PATH)

    filename=nukefileInfo[0]
    nukefilename=nukefileInfo[1]
    nukefilepath=PATH+'/'+nukefilename
    filestart=nukefileInfo[2]
    filelast=nukefileInfo[3]
    read1['file'].setValue(nukefilepath)

    read1['first'].setValue(filestart)
    read1['last'].setValue(filelast)

    reformat=nuke.createNode('Reformat')
    reformat.setInput(0,read1)


    slate=nuke.createNode("slate02")
    slate.setInput(0,reformat)
    slatebtn = slate.knob('set')
    slatebtn.execute()

    write1=nuke.createNode("Write")
    write1.setName('AAAA')
    movname=filename+'.mov'
    writepath=OUTPATH+'/'+movname

    write1['file'].setValue(writepath)

    nuke.execute('AAAA',filestart,filelast,1)


if __name__ == '__main__':
    convertMov(sys.argv[1],sys.argv[2],sys.argv[3])